{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Rutishauser–Romberg 数值导数收敛三角的计算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 创建时间：2021-02-26"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这一份简单笔记会介绍 (Generalized) Rutishauser–Romberg 数值导数收敛三角的 Python 程序的使用与原理。后文会简称 Rutishauser–Romberg 为 RR，Generalized RR 为 GRR。\n",
    "\n",
    "由于 GRR 方法与 RR 方法仅仅差别在等比数列的比例上，因此不加赘述。我们只讨论 RR 收敛三角。\n",
    "\n",
    "这篇文档的复现对象是 Medved et al. [^Medved-Perpete.JMST.2007]。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import findiff\n",
    "import pandas as pd\n",
    "\n",
    "np.set_printoptions(precision=4, suppress=True, linewidth=120)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 问题阐述"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在我们想要对函数 $f(x) = \\sin(x - 0.5)$ 求其在零点处的三阶导数 $f^{(3)}(0)$ 的数值结果。\n",
    "\n",
    "我们假设上述问题有如下限制：\n",
    "\n",
    "- $f(x)$ 的值可以直接获得，但 $f$ 的任意阶导数不能直接获得；\n",
    "\n",
    "- $f(x)$ 只能通过单浮点精度计算机计算 (即存在较大机器精度误差)。\n",
    "\n",
    "理论上，最理想的结果会是 $- \\cos(- 0.5) \\simeq -0.877583$。但在只能通过数值导数计算的情况下，这个值并不是轻易可以得到并且信任的。\n",
    "\n",
    "这样模型问题看起来比较蠢。但现实中，在遇到需要数值地求取高阶导数，却无法保证函数本身精度的情况下，这种分析模式就非常关键。例子是量子化学中的高阶极化率数值计算问题。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    return np.array(np.array(np.sin(x-0.5), dtype=np.float32), dtype=float)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 问题的困难：过低或过高的间隔的数值误差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对于三阶导数问题，采用五点差分时，\n",
    "\n",
    "$$\n",
    "f^{(3)} (x) \\simeq \\frac{1}{h^3} \\left[ - \\frac{1}{2} f(x-2h) + f(x-h) - f(x+h) + \\frac{1}{2} f(x+2h) \\right]\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "原则上，数值导数的间隔 $h$ (interval) 越小越好。如果 $h$ 过大 (譬如取 $h = 0.5$)，那么结果就会与理论值 (-0.877583) 相差很大："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.8240854740142822"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, h = 0, 0.5\n",
    "1/h**3 * (-0.5*f(x-2*h) + f(x-h) - f(x+h) + 0.5*f(x+2*h))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "但如果间隔 $h$ 太小 (譬如取 $h = 0.005$)，那么又会出于机器精度，反而会有更大的偏差："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.9536743164062499"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, h = 0, 0.005\n",
    "1/h**3 * (-0.5*f(x-2*h) + f(x-h) - f(x+h) + 0.5*f(x+2*h))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "因此，间隔 $h$ 必须要适中 (譬如取 $h = 0.05$)，才有可能得到正确的结果："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.877261161804199"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, h = 0, 0.05\n",
    "1/h**3 * (-0.5*f(x-2*h) + f(x-h) - f(x+h) + 0.5*f(x+2*h))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "不仅如此，如果采用下述七点差分公式时，精度或许 (也或许不) 会有些许提升：\n",
    "\n",
    "$$\n",
    "f^{(3)} (x) \\simeq \\frac{1}{48 h^3} \\left[ f(x-4h) - 34 f(x-2h) + 64 f(x-h) - 64 f(x+h) + 34 f(x+2h) - f(x+4h) \\right]\n",
    "$$\n",
    "\n",
    "差分点数量越多，一般结果越精确。但这同时也受限于方才提到的大间隔或机器精度的问题。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.8778721094131468"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1/h**3 * 1/48 * (f(x-4*h) - 34*f(x-2*h) + 64*f(x-h) - 64*f(x+h) + 34*f(x+2*h) - f(x+4*h))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "因此，随即而来的问题是，\n",
    "\n",
    "- 间隔 $h$ 在什么情况下可以被信任，可能得到正确的结果？\n",
    "\n",
    "- 采用几点差分会有更好的效果？"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 问题的解决：RR 收敛三角"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过上面简单的分析，我们知道合理的间隔在 $0.005$ 到 $0.5$ 之间。因此，我们设计以两倍为间隔的等比数列，称为偏移值数列 (英文一般称为 offsets) `offsets_half`："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.004, 0.008, 0.016, 0.032, 0.064, 0.128, 0.256, 0.512, 1.024, 2.048])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "offsets_half = np.array([0.004 * 2**n for n in range(10)])\n",
    "offsets_half"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们对这些数，求取 $f(x)$，生成的数组为 `fx_pos` $[f(0.004), f(0.008), \\cdots, f(2.048)]$："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.4759, -0.4724, -0.4653, -0.4511, -0.4223, -0.3635, -0.2416,  0.012 ,  0.5003,  0.9997])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fx_pos = f(offsets_half)\n",
    "fx_pos"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们同时求取 $f(-x)$，生成的数组为 `fx_neg` $[f(-0.004), f(-0.008), \\cdots, f(-2.048)]$："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.4829, -0.4864, -0.4934, -0.5073, -0.5346, -0.5875, -0.686 , -0.8479, -0.9989, -0.5593])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fx_neg = f(-offsets_half)\n",
    "fx_neg"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "最后需要求取 $f(0)$ `f0`："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(-0.4794)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f0 = f(0)\n",
    "f0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过下述程序给出并绘制 RR 收敛三角。其中 `grrtrig` 程序可供下载 {download}`grrtrig.py`，而大多数函数也会在本文档重新说明。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_81ff4_row3_col1,#T_81ff4_row3_col2,#T_81ff4_row3_col3,#T_81ff4_row4_col1,#T_81ff4_row4_col2,#T_81ff4_row4_col3,#T_81ff4_row5_col2{\n",
       "            background-color:  lightgreen;\n",
       "        }</style><table id=\"T_81ff4_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >0</th>        <th class=\"col_heading level0 col1\" >1</th>        <th class=\"col_heading level0 col2\" >2</th>        <th class=\"col_heading level0 col3\" >3</th>        <th class=\"col_heading level0 col4\" >4</th>        <th class=\"col_heading level0 col5\" >5</th>        <th class=\"col_heading level0 col6\" >6</th>        <th class=\"col_heading level0 col7\" >7</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_81ff4_level0_row0\" class=\"row_heading level0 row0\" >0.004</th>\n",
       "                        <td id=\"T_81ff4_row0_col0\" class=\"data row0 col0\" >-0.931323</td>\n",
       "                        <td id=\"T_81ff4_row0_col1\" class=\"data row0 col1\" >-0.941024</td>\n",
       "                        <td id=\"T_81ff4_row0_col2\" class=\"data row0 col2\" >-0.943126</td>\n",
       "                        <td id=\"T_81ff4_row0_col3\" class=\"data row0 col3\" >-0.943630</td>\n",
       "                        <td id=\"T_81ff4_row0_col4\" class=\"data row0 col4\" >-0.943755</td>\n",
       "                        <td id=\"T_81ff4_row0_col5\" class=\"data row0 col5\" >-0.943786</td>\n",
       "                        <td id=\"T_81ff4_row0_col6\" class=\"data row0 col6\" >-0.943793</td>\n",
       "                        <td id=\"T_81ff4_row0_col7\" class=\"data row0 col7\" >-0.943795</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_81ff4_level0_row1\" class=\"row_heading level0 row1\" >0.008</th>\n",
       "                        <td id=\"T_81ff4_row1_col0\" class=\"data row1 col0\" >-0.902219</td>\n",
       "                        <td id=\"T_81ff4_row1_col1\" class=\"data row1 col1\" >-0.909495</td>\n",
       "                        <td id=\"T_81ff4_row1_col2\" class=\"data row1 col2\" >-0.911364</td>\n",
       "                        <td id=\"T_81ff4_row1_col3\" class=\"data row1 col3\" >-0.911835</td>\n",
       "                        <td id=\"T_81ff4_row1_col4\" class=\"data row1 col4\" >-0.911953</td>\n",
       "                        <td id=\"T_81ff4_row1_col5\" class=\"data row1 col5\" >-0.911982</td>\n",
       "                        <td id=\"T_81ff4_row1_col6\" class=\"data row1 col6\" >-0.911989</td>\n",
       "                        <td id=\"T_81ff4_row1_col7\" class=\"data row1 col7\" ></td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_81ff4_level0_row2\" class=\"row_heading level0 row2\" >0.016</th>\n",
       "                        <td id=\"T_81ff4_row2_col0\" class=\"data row2 col0\" >-0.880391</td>\n",
       "                        <td id=\"T_81ff4_row2_col1\" class=\"data row2 col1\" >-0.881452</td>\n",
       "                        <td id=\"T_81ff4_row2_col2\" class=\"data row2 col2\" >-0.881722</td>\n",
       "                        <td id=\"T_81ff4_row2_col3\" class=\"data row2 col3\" >-0.881791</td>\n",
       "                        <td id=\"T_81ff4_row2_col4\" class=\"data row2 col4\" >-0.881808</td>\n",
       "                        <td id=\"T_81ff4_row2_col5\" class=\"data row2 col5\" >-0.881813</td>\n",
       "                        <td id=\"T_81ff4_row2_col6\" class=\"data row2 col6\" ></td>\n",
       "                        <td id=\"T_81ff4_row2_col7\" class=\"data row2 col7\" ></td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_81ff4_level0_row3\" class=\"row_heading level0 row3\" >0.032</th>\n",
       "                        <td id=\"T_81ff4_row3_col0\" class=\"data row3 col0\" >-0.877208</td>\n",
       "                        <td id=\"T_81ff4_row3_col1\" class=\"data row3 col1\" >-0.877397</td>\n",
       "                        <td id=\"T_81ff4_row3_col2\" class=\"data row3 col2\" >-0.877388</td>\n",
       "                        <td id=\"T_81ff4_row3_col3\" class=\"data row3 col3\" >-0.877386</td>\n",
       "                        <td id=\"T_81ff4_row3_col4\" class=\"data row3 col4\" >-0.877386</td>\n",
       "                        <td id=\"T_81ff4_row3_col5\" class=\"data row3 col5\" ></td>\n",
       "                        <td id=\"T_81ff4_row3_col6\" class=\"data row3 col6\" ></td>\n",
       "                        <td id=\"T_81ff4_row3_col7\" class=\"data row3 col7\" ></td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_81ff4_level0_row4\" class=\"row_heading level0 row4\" >0.064</th>\n",
       "                        <td id=\"T_81ff4_row4_col0\" class=\"data row4 col0\" >-0.876639</td>\n",
       "                        <td id=\"T_81ff4_row4_col1\" class=\"data row4 col1\" >-0.877527</td>\n",
       "                        <td id=\"T_81ff4_row4_col2\" class=\"data row4 col2\" >-0.877527</td>\n",
       "                        <td id=\"T_81ff4_row4_col3\" class=\"data row4 col3\" >-0.877527</td>\n",
       "                        <td id=\"T_81ff4_row4_col4\" class=\"data row4 col4\" ></td>\n",
       "                        <td id=\"T_81ff4_row4_col5\" class=\"data row4 col5\" ></td>\n",
       "                        <td id=\"T_81ff4_row4_col6\" class=\"data row4 col6\" ></td>\n",
       "                        <td id=\"T_81ff4_row4_col7\" class=\"data row4 col7\" ></td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_81ff4_level0_row5\" class=\"row_heading level0 row5\" >0.128</th>\n",
       "                        <td id=\"T_81ff4_row5_col0\" class=\"data row5 col0\" >-0.873975</td>\n",
       "                        <td id=\"T_81ff4_row5_col1\" class=\"data row5 col1\" >-0.877533</td>\n",
       "                        <td id=\"T_81ff4_row5_col2\" class=\"data row5 col2\" >-0.877554</td>\n",
       "                        <td id=\"T_81ff4_row5_col3\" class=\"data row5 col3\" ></td>\n",
       "                        <td id=\"T_81ff4_row5_col4\" class=\"data row5 col4\" ></td>\n",
       "                        <td id=\"T_81ff4_row5_col5\" class=\"data row5 col5\" ></td>\n",
       "                        <td id=\"T_81ff4_row5_col6\" class=\"data row5 col6\" ></td>\n",
       "                        <td id=\"T_81ff4_row5_col7\" class=\"data row5 col7\" ></td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_81ff4_level0_row6\" class=\"row_heading level0 row6\" >0.256</th>\n",
       "                        <td id=\"T_81ff4_row6_col0\" class=\"data row6 col0\" >-0.863299</td>\n",
       "                        <td id=\"T_81ff4_row6_col1\" class=\"data row6 col1\" >-0.877214</td>\n",
       "                        <td id=\"T_81ff4_row6_col2\" class=\"data row6 col2\" ></td>\n",
       "                        <td id=\"T_81ff4_row6_col3\" class=\"data row6 col3\" ></td>\n",
       "                        <td id=\"T_81ff4_row6_col4\" class=\"data row6 col4\" ></td>\n",
       "                        <td id=\"T_81ff4_row6_col5\" class=\"data row6 col5\" ></td>\n",
       "                        <td id=\"T_81ff4_row6_col6\" class=\"data row6 col6\" ></td>\n",
       "                        <td id=\"T_81ff4_row6_col7\" class=\"data row6 col7\" ></td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_81ff4_level0_row7\" class=\"row_heading level0 row7\" >0.512</th>\n",
       "                        <td id=\"T_81ff4_row7_col0\" class=\"data row7 col0\" >-0.821555</td>\n",
       "                        <td id=\"T_81ff4_row7_col1\" class=\"data row7 col1\" ></td>\n",
       "                        <td id=\"T_81ff4_row7_col2\" class=\"data row7 col2\" ></td>\n",
       "                        <td id=\"T_81ff4_row7_col3\" class=\"data row7 col3\" ></td>\n",
       "                        <td id=\"T_81ff4_row7_col4\" class=\"data row7 col4\" ></td>\n",
       "                        <td id=\"T_81ff4_row7_col5\" class=\"data row7 col5\" ></td>\n",
       "                        <td id=\"T_81ff4_row7_col6\" class=\"data row7 col6\" ></td>\n",
       "                        <td id=\"T_81ff4_row7_col7\" class=\"data row7 col7\" ></td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x1479635c1c40>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import grrtrig\n",
    "grr = grrtrig.calculate_GRR_trig(offsets_half, 3, fx_pos, fx_neg, f0)\n",
    "df, _ = grrtrig.output_pd_grr_trig(offsets_half, grr)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "绿色的值中，存有精确的三阶数值导数 $f^{(3)}(0)$ 的概率比较大。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实现原理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从这里开始仅仅是程序笔记。如果只关心如何使用 RR 收敛三角，上面的文档与程序应当已经足够了。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 给定微小偏移函数值下的任意阶数值导数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "首先是考察下述问题。在给定任意偏移列表 (offsets) $\\{ a_m \\}$ 或写作向量 $\\boldsymbol{a}$ 的情况下，其任意阶数值导数 $f^{(n)}$ 应该如何给出？\n",
    "\n",
    "数值导数的形式会是 (其中 $\\{ b_m \\}$ 为待求量，它与导数阶数 $n$ 有关，我们称 $\\{ b_m \\}$ 为数值差分系数 Finite Difference Coefficients)\n",
    "\n",
    "$$\n",
    "f^{(n)} = \\sum_{j = 0}^m b_j f(a_j)\n",
    "$$\n",
    "\n",
    "举例来说，五点差分的三阶导数情况下，$m = 5, n = 3$，并且 $\\{ a_m \\} = \\{ -2, -1, 0, 1, 2 \\}$。那么由此得到的 $\\{ b_m \\} = \\{ - 1/2, 1, 0, -1, 1/2 \\}$。但这是在间隔 $h = 1$ 的情形下给出的。如果是其他间隔，那么\n",
    "\n",
    "$$\n",
    "\\{ a_m \\} = \\{ -2h, -h, 0, h, 2h \\} \\\\\n",
    "\\{ b_m \\} = \\{ - h^{-3}/2, h^{-3}, 0, -h^{-3}, h^{-3}/2 \\}\n",
    "$$\n",
    "\n",
    "这类数值差分系数可以很容易地从 [Wikipedia: Finite difference coefficient](https://en.wikipedia.org/wiki/Finite_difference_coefficient) 上获得。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "但我们实际会遇到的是更复杂的差分情形。譬如方才提到的三阶导数的七点差分。一般的七点差分是从 -3 到 3 的等差数列；但我们方才用到的却是从 -4 到 4 的正负两条等比数列。这要求我们能对任意情形的偏移列表 $\\{ a_m \\}$ 进行系数 $\\{ b_m \\}$ 的计算。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "其计算原理这里不详细展开。详情参考 [Crtaylor 的交互网页](https://web.media.mit.edu/~crtaylor/calculator.html)。实现过程非常简单。\n",
    "\n",
    "- $m \\times m$ 矩阵 $\\mathbf{M}$，其中矩阵元 $M_{ij} = a_j^i$。\n",
    "\n",
    "- $m$ 维度向量 $\\boldsymbol{r}$，其中 $r_n = n!$，其余值均为零。\n",
    "\n",
    "随后求解 $\\mathbf{M} \\boldsymbol{b} = \\boldsymbol{r}$ 即可。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculate_findiff_coefs(offsets, deriv):\n",
    "    \"\"\"\n",
    "    Calculate Finite Difference Coefficients\n",
    "    \n",
    "    Parameters\n",
    "    ----------\n",
    "    offsets : array_like\n",
    "    deriv : int\n",
    "    \n",
    "    Returns\n",
    "    -------\n",
    "    coefs : ndarray\n",
    "    \n",
    "    Examples\n",
    "    --------\n",
    "    >>> calculate_findiff_coefs([-2, -1, 0, 1, 2], 4) \n",
    "    array([ 1., -4.,  6., -4.,  1.])\n",
    "    \n",
    "    >>> calculate_findiff_coefs([-2, -1, 0, 1, 2, 1.99], 3)\n",
    "    array([  -0.1867,   -0.6722,    3.7688,   -6.0505, -124.5   ,  127.6406])\n",
    "    \n",
    "    References\n",
    "    ----------\n",
    "    https://web.media.mit.edu/~crtaylor/calculator.html\n",
    "    https://github.com/maroba/findiff/blob/e8ca33707e3e25d76bf0f93b2391e466209287b1/findiff/coefs.py\n",
    "    \"\"\"\n",
    "    \n",
    "    if len(offsets) < deriv + 1:\n",
    "        raise ValueError(\"Length of offsets should be larger than derivative order plus 1.\")\n",
    "    if len(offsets) != len(set(offsets)):\n",
    "        # Note that this program could not handle pathological cases, only make simple check instead.\n",
    "        raise ValueError(\"Possibly exactly same offset value is given. Please check `offsets'.\")\n",
    "    \n",
    "    offsets = np.asarray(offsets)\n",
    "    matrix = np.array([offsets**n for n in range(len(offsets))])\n",
    "    rhs = np.zeros(len(offsets))\n",
    "    rhs[deriv] = np.math.factorial(deriv)\n",
    "    \n",
    "    return np.linalg.solve(matrix, rhs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### GRR 收敛三角具体实现"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里根据 Medveď 文章式 (15) 来实现。GRR 收敛三角的首列通过上述程序进行数值差分。如果我们令 GRR 收敛三角为矩阵 $\\mathbf{P}$，其矩阵元用 $P_{r, c}$ 表示，那么 $c >= 1$ 时，\n",
    "\n",
    "$$\n",
    "P_{r, c} = \\frac{a^{2c} P_{r, c-1} - P_{r+1, c-1}}{a^{2c} - 1}\n",
    "$$\n",
    "\n",
    "以此方式迭代即可。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "事实上，如果收敛三角任意列可以全部使用上述 `calculate_findiff_coefs` 函数求得，并且与迭代表达式等价。但出于数值稳定性考虑，仍然选择使用迭代式。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculate_GRR_trig(offsets_half, deriv, fx_pos, fx_neg, f0=None):\n",
    "    \"\"\"\n",
    "    Calculate (Generalized) Rutishauser–Romberg Triangle\n",
    "    \n",
    "    Parameters\n",
    "    ----------\n",
    "    offsets_half : array_like\n",
    "        Positive half part of offsets.  For example, if all offsets are [-2, -1, 0, 1, 2],\n",
    "            then `offsets_half' should be [1, 2].\n",
    "        Must be a geometric sequence. This function does not make double check on this.\n",
    "        Do not contain zero in this array.\n",
    "    deriv : int\n",
    "    fx_pos : array_like\n",
    "        Value list of f(offsets_half). Dimension should be the same to `offsets_half'.\n",
    "    fx_neg : array_like\n",
    "        Value list of f(-offsets_half). Dimension should be the same to `offsets_half'.\n",
    "    f0 : float or None\n",
    "        Value of f(0). May leave as None if derivative order is odd number.\n",
    "    \n",
    "    Returns\n",
    "    -------\n",
    "    grr_trig : ndarray\n",
    "        (Generalized) Rutishauser–Romberg triangle.\n",
    "    \"\"\"\n",
    "    \n",
    "    if deriv % 2 == 0 and f0 is None:\n",
    "        raise ValueError(\"`f0' should be provided if derivative order is even number.\")\n",
    "    else:\n",
    "        f0 = 0 if f0 is None else f0\n",
    "    if len(offsets_half) < 2:\n",
    "        raise ValueError(\"length of `offsets_half' must >= 2 in order to calculate ratio.\")\n",
    "\n",
    "    comp_len = (deriv + 1) // 2\n",
    "    mat_size = len(offsets_half) - comp_len\n",
    "    grr_trig = np.zeros((mat_size, mat_size))\n",
    "    ratio = offsets_half[-1] / offsets_half[-2]\n",
    "    \n",
    "    for r in range(mat_size):\n",
    "        i_end = r + comp_len\n",
    "        offsets = np.concatenate([offsets_half[r:i_end], -offsets_half[r:i_end], [0]])\n",
    "        coef_list = calculate_findiff_coefs(offsets, deriv)\n",
    "        val_list = np.concatenate([fx_pos[r:i_end], fx_neg[r:i_end], [f0]])\n",
    "        grr_trig[r, 0] = (coef_list * val_list).sum()\n",
    "    for c in range(1, mat_size):\n",
    "        for r in range(mat_size-c):\n",
    "            grr_trig[r, c] = (ratio**(2*c) * grr_trig[r, c-1] - grr_trig[r+1, c-1]) / (ratio**(2*c) - 1)    \n",
    "            \n",
    "    for r in range(mat_size):\n",
    "        for c in range(mat_size-r, mat_size):\n",
    "            grr_trig[r, c] = np.nan\n",
    "    return grr_trig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对于文档开头 $f(x) = \\sin(x - 0.5)$ 的问题，其 RR 收敛三角为"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.9313, -0.941 , -0.9431, -0.9436, -0.9438, -0.9438, -0.9438, -0.9438],\n",
       "       [-0.9022, -0.9095, -0.9114, -0.9118, -0.912 , -0.912 , -0.912 ,     nan],\n",
       "       [-0.8804, -0.8815, -0.8817, -0.8818, -0.8818, -0.8818,     nan,     nan],\n",
       "       [-0.8772, -0.8774, -0.8774, -0.8774, -0.8774,     nan,     nan,     nan],\n",
       "       [-0.8766, -0.8775, -0.8775, -0.8775,     nan,     nan,     nan,     nan],\n",
       "       [-0.874 , -0.8775, -0.8776,     nan,     nan,     nan,     nan,     nan],\n",
       "       [-0.8633, -0.8772,     nan,     nan,     nan,     nan,     nan,     nan],\n",
       "       [-0.8216,     nan,     nan,     nan,     nan,     nan,     nan,     nan]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "calculate_GRR_trig(offsets_half, 3, fx_pos, fx_neg, f0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实现补充"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### GRR 收敛三角的合理收敛值确定"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果不考虑机器精度所导致的误差，那么收敛三角的最右上方一定是精度 (accuracy) 阶数最高的结果。但很显然，事实是这个值反而是偏差最大的点。因此，我们需要问，上述三角的那个结果是可以信任的？\n",
    "\n",
    "一般来说，与周围数值偏差最小的点是可以信任的。上述三角的第 1-4 列、第 3-6 行看起来比较可靠。但我们需要一种方法较为系统地评价值是否可以信任。\n",
    "\n",
    "这里的评价标准是与下方和左方的数值进行比较；如果误差较小，那么就可以信任。因此，首列与对角线没有被纳入评判。事实上，对角线值往往更准确，因此最终评价的时候还是需要人看看周围的数值是否合理。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "def check_grr_trig_converge(grr_trig):\n",
    "    \"\"\"\n",
    "    Convergence Check Matrix of (Generalized) Rutishauser–Romberg Triangle\n",
    "    \n",
    "    Parameters\n",
    "    ----------\n",
    "    grr_trig : ndarray\n",
    "        (Generalized) Rutishauser–Romberg triangle.\n",
    "    \n",
    "    Return\n",
    "    ------\n",
    "    mat_chk : ndarray\n",
    "    \"\"\"\n",
    "    \n",
    "    n = len(grr_trig)\n",
    "    mat_chk = np.zeros((n, n))\n",
    "    for r in range(0, n-1):\n",
    "        for c in range(1, n-r-1):\n",
    "            mat_chk[r, c] = np.abs(grr_trig[r, c] - grr_trig[r+1, c]) + np.abs(grr_trig[r, c] - grr_trig[r, c-1])\n",
    "    for r in range(n):\n",
    "        mat_chk[r, 0] = np.nan\n",
    "    for r in range(0, n):\n",
    "        for c in range(n-r-1, n):\n",
    "            mat_chk[r, c] = np.nan\n",
    "    return mat_chk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   nan, 0.0412, 0.0339, 0.0323, 0.0319, 0.0318, 0.0318,    nan],\n",
       "       [   nan, 0.0353, 0.0315, 0.0305, 0.0303, 0.0302,    nan,    nan],\n",
       "       [   nan, 0.0051, 0.0046, 0.0045, 0.0044,    nan,    nan,    nan],\n",
       "       [   nan, 0.0003, 0.0001, 0.0001,    nan,    nan,    nan,    nan],\n",
       "       [   nan, 0.0009, 0.    ,    nan,    nan,    nan,    nan,    nan],\n",
       "       [   nan, 0.0039,    nan,    nan,    nan,    nan,    nan,    nan],\n",
       "       [   nan,    nan,    nan,    nan,    nan,    nan,    nan,    nan],\n",
       "       [   nan,    nan,    nan,    nan,    nan,    nan,    nan,    nan]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grr = calculate_GRR_trig(offsets_half, 3, fx_pos, fx_neg, f0)\n",
    "check_grr_trig_converge(grr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### GRR 收敛三角在 Pandas 的绘制"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上述结果可以通过图像表格呈现。浅绿色打底的单元格是上面给出的误差最小的若干单元格附近的格点；其数值导数可以被相信的概率较大。这些单元格外围一圈也有值得考虑的价值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def output_pd_grr_trig(offsets_half, grr_trig, tolerance=3):\n",
    "    \"\"\"\n",
    "    Pandas Presentation of (Generalized) Rutishauser–Romberg Triangle\n",
    "    \n",
    "    Parameters\n",
    "    ----------\n",
    "    offsets_half : array_like\n",
    "    grr_trig : ndarray\n",
    "        (Generalized) Rutishauser–Romberg triangle.\n",
    "    tolerance : int\n",
    "        Number of minimum difference cells in convergence check matrix.\n",
    "    \n",
    "    Return\n",
    "    ------\n",
    "    df : pandas.io.formats.style.Styler\n",
    "        Pandas show of GRR triangle.\n",
    "    df_check : pandas.io.formats.style.Styler\n",
    "        Pandas show of convergence check matrix of GRR triangle.\n",
    "    \"\"\"\n",
    "    n = len(grr_trig)\n",
    "    df = pd.DataFrame(grr_trig, columns=range(n), index=offsets_half[:n])\n",
    "    df_check = pd.DataFrame(check_grr_trig_converge(grr_trig), columns=range(n), index=offsets_half[:n])\n",
    "    df.replace(np.nan, \"\", regex=True, inplace=True)\n",
    "    df_check.replace(np.nan, \"\", regex=True, inplace=True)\n",
    "\n",
    "    t = check_grr_trig_converge(grr_trig).flatten()\n",
    "    t = t.argsort()[:3]\n",
    "    t = np.array([t // n, t % n]).T\n",
    "    \n",
    "    def highlight_cells(x):\n",
    "        df = x.copy()\n",
    "        df.loc[:,:] = '' \n",
    "        for r, c in t:\n",
    "            df.iloc[r, c] = \"background-color: lightgreen\"\n",
    "            df.iloc[r, c-1] = \"background-color: lightgreen\"\n",
    "            df.iloc[r+1, c] = \"background-color: lightgreen\"\n",
    "        return df\n",
    "\n",
    "    df = df.style.apply(highlight_cells, axis=None)\n",
    "    df_check = df_check.style.apply(highlight_cells, axis=None)\n",
    "    return df, df_check"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style  type=\"text/css\" >\n",
       "#T_2c20e_row3_col1,#T_2c20e_row3_col2,#T_2c20e_row3_col3,#T_2c20e_row4_col1,#T_2c20e_row4_col2,#T_2c20e_row4_col3,#T_2c20e_row5_col2{\n",
       "            background-color:  lightgreen;\n",
       "        }</style><table id=\"T_2c20e_\" ><thead>    <tr>        <th class=\"blank level0\" ></th>        <th class=\"col_heading level0 col0\" >0</th>        <th class=\"col_heading level0 col1\" >1</th>        <th class=\"col_heading level0 col2\" >2</th>        <th class=\"col_heading level0 col3\" >3</th>        <th class=\"col_heading level0 col4\" >4</th>        <th class=\"col_heading level0 col5\" >5</th>        <th class=\"col_heading level0 col6\" >6</th>        <th class=\"col_heading level0 col7\" >7</th>    </tr></thead><tbody>\n",
       "                <tr>\n",
       "                        <th id=\"T_2c20e_level0_row0\" class=\"row_heading level0 row0\" >0.004</th>\n",
       "                        <td id=\"T_2c20e_row0_col0\" class=\"data row0 col0\" >-0.931323</td>\n",
       "                        <td id=\"T_2c20e_row0_col1\" class=\"data row0 col1\" >-0.941024</td>\n",
       "                        <td id=\"T_2c20e_row0_col2\" class=\"data row0 col2\" >-0.943126</td>\n",
       "                        <td id=\"T_2c20e_row0_col3\" class=\"data row0 col3\" >-0.943630</td>\n",
       "                        <td id=\"T_2c20e_row0_col4\" class=\"data row0 col4\" >-0.943755</td>\n",
       "                        <td id=\"T_2c20e_row0_col5\" class=\"data row0 col5\" >-0.943786</td>\n",
       "                        <td id=\"T_2c20e_row0_col6\" class=\"data row0 col6\" >-0.943793</td>\n",
       "                        <td id=\"T_2c20e_row0_col7\" class=\"data row0 col7\" >-0.943795</td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_2c20e_level0_row1\" class=\"row_heading level0 row1\" >0.008</th>\n",
       "                        <td id=\"T_2c20e_row1_col0\" class=\"data row1 col0\" >-0.902219</td>\n",
       "                        <td id=\"T_2c20e_row1_col1\" class=\"data row1 col1\" >-0.909495</td>\n",
       "                        <td id=\"T_2c20e_row1_col2\" class=\"data row1 col2\" >-0.911364</td>\n",
       "                        <td id=\"T_2c20e_row1_col3\" class=\"data row1 col3\" >-0.911835</td>\n",
       "                        <td id=\"T_2c20e_row1_col4\" class=\"data row1 col4\" >-0.911953</td>\n",
       "                        <td id=\"T_2c20e_row1_col5\" class=\"data row1 col5\" >-0.911982</td>\n",
       "                        <td id=\"T_2c20e_row1_col6\" class=\"data row1 col6\" >-0.911989</td>\n",
       "                        <td id=\"T_2c20e_row1_col7\" class=\"data row1 col7\" ></td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_2c20e_level0_row2\" class=\"row_heading level0 row2\" >0.016</th>\n",
       "                        <td id=\"T_2c20e_row2_col0\" class=\"data row2 col0\" >-0.880391</td>\n",
       "                        <td id=\"T_2c20e_row2_col1\" class=\"data row2 col1\" >-0.881452</td>\n",
       "                        <td id=\"T_2c20e_row2_col2\" class=\"data row2 col2\" >-0.881722</td>\n",
       "                        <td id=\"T_2c20e_row2_col3\" class=\"data row2 col3\" >-0.881791</td>\n",
       "                        <td id=\"T_2c20e_row2_col4\" class=\"data row2 col4\" >-0.881808</td>\n",
       "                        <td id=\"T_2c20e_row2_col5\" class=\"data row2 col5\" >-0.881813</td>\n",
       "                        <td id=\"T_2c20e_row2_col6\" class=\"data row2 col6\" ></td>\n",
       "                        <td id=\"T_2c20e_row2_col7\" class=\"data row2 col7\" ></td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_2c20e_level0_row3\" class=\"row_heading level0 row3\" >0.032</th>\n",
       "                        <td id=\"T_2c20e_row3_col0\" class=\"data row3 col0\" >-0.877208</td>\n",
       "                        <td id=\"T_2c20e_row3_col1\" class=\"data row3 col1\" >-0.877397</td>\n",
       "                        <td id=\"T_2c20e_row3_col2\" class=\"data row3 col2\" >-0.877388</td>\n",
       "                        <td id=\"T_2c20e_row3_col3\" class=\"data row3 col3\" >-0.877386</td>\n",
       "                        <td id=\"T_2c20e_row3_col4\" class=\"data row3 col4\" >-0.877386</td>\n",
       "                        <td id=\"T_2c20e_row3_col5\" class=\"data row3 col5\" ></td>\n",
       "                        <td id=\"T_2c20e_row3_col6\" class=\"data row3 col6\" ></td>\n",
       "                        <td id=\"T_2c20e_row3_col7\" class=\"data row3 col7\" ></td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_2c20e_level0_row4\" class=\"row_heading level0 row4\" >0.064</th>\n",
       "                        <td id=\"T_2c20e_row4_col0\" class=\"data row4 col0\" >-0.876639</td>\n",
       "                        <td id=\"T_2c20e_row4_col1\" class=\"data row4 col1\" >-0.877527</td>\n",
       "                        <td id=\"T_2c20e_row4_col2\" class=\"data row4 col2\" >-0.877527</td>\n",
       "                        <td id=\"T_2c20e_row4_col3\" class=\"data row4 col3\" >-0.877527</td>\n",
       "                        <td id=\"T_2c20e_row4_col4\" class=\"data row4 col4\" ></td>\n",
       "                        <td id=\"T_2c20e_row4_col5\" class=\"data row4 col5\" ></td>\n",
       "                        <td id=\"T_2c20e_row4_col6\" class=\"data row4 col6\" ></td>\n",
       "                        <td id=\"T_2c20e_row4_col7\" class=\"data row4 col7\" ></td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_2c20e_level0_row5\" class=\"row_heading level0 row5\" >0.128</th>\n",
       "                        <td id=\"T_2c20e_row5_col0\" class=\"data row5 col0\" >-0.873975</td>\n",
       "                        <td id=\"T_2c20e_row5_col1\" class=\"data row5 col1\" >-0.877533</td>\n",
       "                        <td id=\"T_2c20e_row5_col2\" class=\"data row5 col2\" >-0.877554</td>\n",
       "                        <td id=\"T_2c20e_row5_col3\" class=\"data row5 col3\" ></td>\n",
       "                        <td id=\"T_2c20e_row5_col4\" class=\"data row5 col4\" ></td>\n",
       "                        <td id=\"T_2c20e_row5_col5\" class=\"data row5 col5\" ></td>\n",
       "                        <td id=\"T_2c20e_row5_col6\" class=\"data row5 col6\" ></td>\n",
       "                        <td id=\"T_2c20e_row5_col7\" class=\"data row5 col7\" ></td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_2c20e_level0_row6\" class=\"row_heading level0 row6\" >0.256</th>\n",
       "                        <td id=\"T_2c20e_row6_col0\" class=\"data row6 col0\" >-0.863299</td>\n",
       "                        <td id=\"T_2c20e_row6_col1\" class=\"data row6 col1\" >-0.877214</td>\n",
       "                        <td id=\"T_2c20e_row6_col2\" class=\"data row6 col2\" ></td>\n",
       "                        <td id=\"T_2c20e_row6_col3\" class=\"data row6 col3\" ></td>\n",
       "                        <td id=\"T_2c20e_row6_col4\" class=\"data row6 col4\" ></td>\n",
       "                        <td id=\"T_2c20e_row6_col5\" class=\"data row6 col5\" ></td>\n",
       "                        <td id=\"T_2c20e_row6_col6\" class=\"data row6 col6\" ></td>\n",
       "                        <td id=\"T_2c20e_row6_col7\" class=\"data row6 col7\" ></td>\n",
       "            </tr>\n",
       "            <tr>\n",
       "                        <th id=\"T_2c20e_level0_row7\" class=\"row_heading level0 row7\" >0.512</th>\n",
       "                        <td id=\"T_2c20e_row7_col0\" class=\"data row7 col0\" >-0.821555</td>\n",
       "                        <td id=\"T_2c20e_row7_col1\" class=\"data row7 col1\" ></td>\n",
       "                        <td id=\"T_2c20e_row7_col2\" class=\"data row7 col2\" ></td>\n",
       "                        <td id=\"T_2c20e_row7_col3\" class=\"data row7 col3\" ></td>\n",
       "                        <td id=\"T_2c20e_row7_col4\" class=\"data row7 col4\" ></td>\n",
       "                        <td id=\"T_2c20e_row7_col5\" class=\"data row7 col5\" ></td>\n",
       "                        <td id=\"T_2c20e_row7_col6\" class=\"data row7 col6\" ></td>\n",
       "                        <td id=\"T_2c20e_row7_col7\" class=\"data row7 col7\" ></td>\n",
       "            </tr>\n",
       "    </tbody></table>"
      ],
      "text/plain": [
       "<pandas.io.formats.style.Styler at 0x147937f85be0>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df, df_check = output_pd_grr_trig(offsets_half, grr)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[^Medved-Perpete.JMST.2007]: Medveď, M.; Stachová, M.; Jacquemin, D.; André, J.-M.; Perpète, E. A. A Generalized Romberg Differentiation Procedure for Calculation of Hyperpolarizabilities. *J. Mol. Struct. THEOCHEM* **2007**, *847* (1-3), 39–46. doi: [10.1016/j.theochem.2007.08.028](https://doi.org/10.1016/j.theochem.2007.08.028)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
